const {sync} = require('vuex-router-sync')
const {app, router, store} = require('./app')

sync(store, router)


module.exports = context => {
	router.push(context.url)

	const components = router.getMatchedComponents()

	if (!components.length) {
		return Promise.reject({ code: 404 })
	}

	return Promise.all(
		components.map(
			(c) => {
				if (c.preFetch) {
					return c.preFetch(router.currentRoute, store)
				}
			}
		)
	).then(() => {
		return app
	})
}
